package com.photoeditor.demo.ui.dialog;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WDialog implements IDialog {
    private Class mClz;
    private Context mContext;
    private List<Boolean> mAnds = new ArrayList<>();
    private List<Boolean> mOrs = new ArrayList<>();
    private Map<String, String> mStrMap = new HashMap<>();
    private Map<String, Integer> mIntMap = new HashMap<>();
    private Object mObject;
    private IListener mListener;

    private WDialog(Class clz) {
        mClz = clz;
    }

    public static IDialog create(Class clz) {
        return new WDialog(clz);
    }

    @Override
    public IDialog with(Context context) {
        mContext = context;
        return this;
    }

    @Override
    public IDialog and(Boolean and) {
        mAnds.add(and);
        return this;
    }

    @Override
    public IDialog or(Boolean or) {
        mOrs.add(or);
        return this;
    }

    @Override
    public IDialog paras(String key, String value) {
        mStrMap.put(key, value);
        return this;
    }

    @Override
    public IDialog paras(String key, int value) {
        mIntMap.put(key, value);
        return this;
    }

    @Override
    public IDialog paras(String key, Object listener) {
        mObject = listener;
        return this;
    }

    @Override
    public IDialog listen(IListener listener) {
        mListener = listener;
        return this;
    }

    @Override
    public void show() {
        show(null);
    }

    @Override
    public void show(IShowListener listener) {
        if (!check()) {
            if (listener != null) {
                listener.no();
            }
            return;
        }
        if (mClz == null) {
            if (listener != null) {
                listener.no();
            }
            return;
        }
        /*if (mClz.isAssignableFrom(HighRatingObtainVipDialog.class)) {
            try {
                int paras_type = mIntMap.get("type");

                Object entity = mClz.newInstance();
                Method methodNew =  mClz.getMethod("newInstance", int.class);
                methodNew.invoke(entity, paras_type);

                Method methodShow = mClz.getSuperclass().getDeclaredMethod("show", FragmentTransaction.class, String.class);
                if (mContext instanceof FragmentActivity) {
                    methodShow.invoke(entity, ((FragmentActivity) mContext).getSupportFragmentManager().beginTransaction(), mClz.getSimpleName());
                    if (listener != null) {
                        listener.yes();
                    }
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (listener != null) {
                listener.no();
            }
        } else  */if (mClz.isAssignableFrom(WatchAdvanceObtainResultDialog.class)) {
            try {
                int paras_type = mIntMap.get("type");
                String paras_retain = mStrMap.get("retain");

                Object entity = mClz.newInstance();
                Method methodNew =  mClz.getMethod("newInstance", String.class, int.class, IListener.class);
                methodNew.invoke(entity, paras_retain, paras_type, mListener);

                Method methodShow = mClz.getSuperclass().getDeclaredMethod("show", FragmentTransaction.class, String.class);
                if (mContext instanceof FragmentActivity) {
                    methodShow.invoke(entity, ((FragmentActivity) mContext).getSupportFragmentManager().beginTransaction(), mClz.getSimpleName());
                    if (listener != null) {
                        listener.yes();
                    }
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (listener != null) {
                listener.no();
            }
        } else if (mClz.isAssignableFrom(RateGuide.class)) {
            try {
                String paras_action = mStrMap.get("action");
                int paras_where = mIntMap.get("where");

                Object entity = mClz.newInstance();
                Method methodStart = mClz.getMethod("start", Context.class, String.class, RateGuide.OnRateChageListener.class, IListener.class, int.class);
                methodStart.invoke(entity, mContext, paras_action, mObject, mListener, paras_where);

                if (listener != null) {
                    listener.yes();
                }
                return;
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (listener != null) {
                listener.no();
            }
        }
    }

    @Override
    public Boolean check() {
        Boolean andResult = false;
        if (!mAnds.isEmpty()) {
            andResult = true;
            for (Boolean and : mAnds) {
                andResult = andResult && and;
            }
        }
        Boolean orResult = false;
        if (!mOrs.isEmpty()) {
            for (Boolean or : mOrs) {
                orResult = orResult || or;
            }
        }
        if (mAnds.isEmpty() && mOrs.isEmpty()) {
            return true;
        }
        return andResult || orResult;
    }
}
